package com.attendcollatesys.controller.querycontroller;

import com.attendcollatesys.entity.vo.ATLeaveExtraDetailVO;
import com.attendcollatesys.entity.vo.OverTimeResultVo;
import com.attendcollatesys.ex.handler.ex.MessageNullValueException;
import com.attendcollatesys.mapper.ATDayDetailMapper;
import com.attendcollatesys.service.ATLeaveExtraDetailService;
import com.attendcollatesys.service.AtteResultsService;
import com.attendcollatesys.web.AppHttpCodeEnum;
import com.attendcollatesys.web.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("atteresult/")
public class AtteResultsController {

    @Autowired
    AtteResultsService atteResultsService;

    @Autowired
    ATDayDetailMapper atDayDetailMapper;

    @Autowired
    ATLeaveExtraDetailService atLeaveExtraDetailService;

    @GetMapping("queryatte")
    public ResponseResult<?> getDayAttResult(String currentMonth,String name,String rtxNumber) throws ParseException {

        List<Map<String, String>> dayAttResult = atteResultsService.getDayAttResult(currentMonth, name, rtxNumber);
        if (null==dayAttResult) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_UNFIND);
        }
        return ResponseResult.okResult(dayAttResult);
    }


    @GetMapping("workOvertime")
    public ResponseResult<?> getOverTimeResult(String currentMonth, String name, String rtxNumber,int pageNum,int pageSize) throws ParseException {
        List<OverTimeResultVo> overTimeResultVos = atteResultsService.buildOTResult(currentMonth, name, rtxNumber,pageNum,pageSize);
        if (null==overTimeResultVos) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_UNFIND);
        }
        HashMap<String, Object> resultMap = new HashMap<>();
        int count = atDayDetailMapper.countOverTimeByRtxNameDate(currentMonth, name, rtxNumber);
        resultMap.put("total", count);
        resultMap.put("workOvertimeList",overTimeResultVos);
        return ResponseResult.okResult(resultMap);
    }

    @PostMapping("abnormal")
    public ResponseResult<?> getAbnormalAttendanceResult(@RequestBody Map<String,Object> x) throws ParseException, MessageNullValueException {
        Map<String,String> params = new HashMap<>();
        params.put("currentMonth", (String) x.get("currentMonth"));
        params.put("name",(String) x.get("name"));
        params.put("rtxNumber",(String) x.get("rtxNumber"));
        params.put("pageNum",x.get("pageNum").toString());
        params.put("pageSize", x.get("pageSize").toString());
        ATLeaveExtraDetailVO atLeaveExtraDetailVO = atLeaveExtraDetailService.selectAllAbnormal(params);
        if (null==atLeaveExtraDetailVO) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_UNFIND);
        }
        return ResponseResult.okResult(atLeaveExtraDetailVO);
    }


}
